package com.loadmore.common.net.impl

import android.text.TextUtils
import com.alibaba.fastjson.JSON
import com.lzy.okgo.callback.AbsCallback
import okhttp3.Response
import java.lang.reflect.ParameterizedType

/**
 *@author sunleilei
 *@date 2018/7/12
 *@time 8:35
 *
 */
abstract class JsonCallback<T> :AbsCallback<T>() {
    override fun convertResponse(response: Response?): T ?{

        var content = response?.body()?.string()

        var parameterizedType = javaClass.genericSuperclass as ParameterizedType
        var type = parameterizedType.actualTypeArguments[0]
        if (!TextUtils.isEmpty(content)) {
            if (content!!.startsWith("{") && content.endsWith("}")) {

                return JSON.parseObject(content,type)
            }else if (content!!.startsWith("[") && content.endsWith("]")) {
                return JSON.parseArray(content, arrayOf(type)) as T
            }
        }

        return null
    }
}